<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\AdminUsers;
use App\Models\SLessonFeedback;
use App\Models\SStuTaskInfo;
use App\Models\STaskInfo;
use App\Models\STeacherInfo;
use App\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class StuTaskController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('学生任务')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('学生任务')
			->description('详情')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('学生任务')
			->description('编辑')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('学生任务')
			->description('新增')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new SStuTaskInfo);

		$grid->id('Id')->sortable();
		$grid->users()->name('学生');
		$grid->task_id('任务')->display(function () {
			if ($this->task_id) {
				return $this->tasks->title;
			} else {
				return $this->feedBacks->title;
			}
		});
		// $grid->teacher_id('Teacher id');
		// $grid->admin_id('Admin id');
		// $grid->feed_back_id('Feed back id');
		$grid->status('状态')->using(config('svs.stu_task_status'));
		$grid->content('提交内容');
		$grid->pics('图片')->image();
		$grid->voices('录音');
		$grid->evaluation('批改内容')->limit(50);
		$grid->submit_time('学生提交时间');
		$grid->audit_time('审核时间');
		$grid->eva_time('点评时间');
		$grid->created_at('创建时间');
		$grid->updated_at('更新时间');

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(SStuTaskInfo::findOrFail($id));

		$show->id('Id');
		$show->user_id('学生')->as(function () {
			return @$this->users->name;
		});

		$show->task_id('任务')->as(function () {
			return @$this->tasks->title;
		});
		$show->teacher_id('老师')->as(function () {
			return @$this->teachers->name;
		});
		$show->admin_id('工作人员')->as(function () {
			return @$this->admins->name;
		});
		$show->feed_back_id('反馈内容')->as(function () {
			return @$this->feedBacks->content;
		});
		$show->status('状态')->using(config('svs.stu_task_status'));
		$show->content('提交内容');
		$show->pics('图片');
		$show->voices('录音');
		$show->evaluation('批改内容');
		$show->submit_time('学生提交时间');
		$show->audit_time('审核时间');
		$show->eva_time('点评时间');
		$show->created_at('创建时间');
		$show->updated_at('更新时间');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new SStuTaskInfo);

		// $users = User::pluck('name', 'id');
		// $form->select('user_id', '学生')->options($users);
		//
		$form->select('user_id', '学生')->options(function ($id) {
			$user = User::find($id);

			if ($user) {
				return [$user->id => $user->name];
			}

		})->ajax('/ajax/users');

		// $tasks = STaskInfo::pluck('title', 'id');
		// $form->select('task_id', '任务')->options($tasks)->default('nullable');
		$form->select('task_id', '任务')->options(function ($id) {
			$user = STaskInfo::find($id);

			if ($user) {
				return [$user->id => $user->title];
			}

		})->ajax('/ajax/tasks');
		// $teachers = STeacherInfo::pluck('name', 'id');
		// $form->select('teacher_id', '老师')->options($teachers)->default('nullable');
		//
		$form->select('teacher_id', '老师')->options(function ($id) {
			$teacher = STeacherInfo::find($id);

			if ($teacher) {
				return [$teacher->id => $teacher->name];
			}

		})->ajax('/ajax/teachers');

		// $admins = AdminUsers::pluck('username', 'id');
		// $form->select('admin_id', '工作人员')->options($admins)->default('nullable');

		$form->select('admin_id', '工作人员')->options(function ($id) {
			$teacher = AdminUsers::find($id);

			if ($teacher) {
				return [$teacher->id => $teacher->username];
			}

		})->ajax('/ajax/admins');

		// $feeds = SLessonFeedback::pluck('title', 'id');
		// $form->select('feed_back_id', '反馈内容')->options($feeds)->default('nullable');

		$form->select('feed_back_id', '反馈内容')->options(function ($id) {
			$teacher = SLessonFeedback::find($id);

			if ($teacher) {
				return [$teacher->id => $teacher->title];
			}

		})->ajax('/ajax/feedbacks');

		$form->select('status', '状态')->options(config('svs.stu_task_status'));
		$form->textarea('content', '提交内容')->default('nullable');
		$form->image('pics', '图片');
		$form->file('voices', '录音');
		$form->textarea('evaluation', '批改内容')->rules(function ($form) {

			// 如果不是编辑状态，则添加字段唯一验证
			if (!$id = $form->model()->id) {
				$form->ignore(['evaluation']);
			}

		});

		$form->saving(function (Form $form) {
			if (!$form->task_id) {
				$form->task_id = 0;
			}
			if (!$form->teacher_id) {
				$form->teacher_id = 0;
			}
			if (!$form->admin_id) {
				$form->admin_id = 0;
			}
			if (!$form->feed_back_id) {
				$form->feed_back_id = 0;
			}

			if ($form->status < 21) {
				$form->ignore(['evaluation']);
			}
		});

		return $form;
	}
}
